package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.service.IWorkflowBackConfigService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/workflowBackConfig", produces = MediaType.APPLICATION_JSON_VALUE)
public class WorkflowBackConfigController  extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(WorkflowBackConfigController.class);
    @Autowired
    private IWorkflowBackConfigService workflowBackConfigService;
    @GetMapping("/queryWorkflowBackConfigPage")
    public ApiResultDTO queryWorkflowBackConfigPage(@RequestHeader("X-Token") String tokenid,
                                                    @RequestParam("page") String page,
                                                    @RequestParam("limit") String limit) {
        try {
            UserDTO userDto = getUser(tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String,Object>> pageDTO = workflowBackConfigService.queryWorkflowBackPage(paginationDTO,userDto.getProvince());
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }

    @PostMapping("/saveWorkflowBackConfig")
    public ApiResultDTO saveWorkflowBackConfig(@RequestHeader("X-Token") String tokenid,
                                               @RequestBody Map<String,Object> param){
        try {
            UserDTO userDto = getUser(tokenid);

        Map<String, Object> params=new HashMap<>();
        params.put("menu_id",param.get("menuid"));
        params.put("menu_name",param.get("menuName"));
        params.put("backto_menu_id",param.get("backmenuid"));
        params.put("backto_menu_name",param.get("backmenuName"));
        params.put("mof_div_code",userDto.getProvince());
        workflowBackConfigService.insertWorkflowBack(params);
        return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }
    @PostMapping("/updateWorkflowBackConfig")
    public ApiResultDTO updateWorkflowBackConfig(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody Map<String,Object> param){
        try {
            UserDTO userDto = getUser(tokenid);

            Map<String, Object> params=new HashMap<>();
            params.put("id",param.get("id"));
            params.put("menu_id",param.get("menuid"));
            params.put("menu_name",param.get("menuName"));
            params.put("backto_menu_id",param.get("backmenuid"));
            params.put("backto_menu_name",param.get("backmenuName"));
            params.put("mof_div_code",userDto.getProvince());
            workflowBackConfigService.updateWorkflowBack(params);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }
    @DeleteMapping("/deleteWorkflowBackConfig/{id}")
    public ApiResultDTO deleteWorkflowBackConfig(@PathVariable("id") String id){
        try {
            workflowBackConfigService.deleteWorkflowBack(id);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除失败！");
        }
    }
}
